import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.List;

/**
 * 房间状态管理面板
 */
public class RoomStatusPanel extends JPanel {
    private JTable roomTable;
    private DefaultTableModel tableModel;
    private JLabel totalRoomsLabel;
    private JLabel availableRoomsLabel;
    private JLabel occupiedRoomsLabel;
    private JLabel reservedRoomsLabel;

    public RoomStatusPanel() {
        setupUI();
        refreshData();
    }

    private void setupUI() {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        setBorder(new EmptyBorder(20, 20, 20, 20));

        // 标题面板
        JPanel titlePanel = new JPanel(new BorderLayout());
        titlePanel.setBackground(Color.WHITE);
        titlePanel.setBorder(new EmptyBorder(0, 0, 20, 0));

        JLabel titleLabel = new JLabel("房间状态管理");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        titleLabel.setForeground(HotelManagementSystem.getPrimaryColor());
        titlePanel.add(titleLabel, BorderLayout.WEST);

        JButton refreshButton = createStyledButton("刷新", HotelManagementSystem.getSecondaryColor());
        refreshButton.addActionListener(e -> refreshData());
        titlePanel.add(refreshButton, BorderLayout.EAST);

        add(titlePanel, BorderLayout.NORTH);

        // 统计面板
        JPanel statsPanel = createStatsPanel();
        add(statsPanel, BorderLayout.CENTER);

        // 房间表格面板
        JPanel tablePanel = createTablePanel();
        add(tablePanel, BorderLayout.SOUTH);
    }

    private JPanel createStatsPanel() {
        JPanel panel = new JPanel(new GridLayout(1, 4, 20, 0));
        panel.setBackground(Color.WHITE);
        panel.setBorder(new EmptyBorder(0, 0, 20, 0));

        // 总房间数
        JPanel totalPanel = createStatCard("总房间数", "0", HotelManagementSystem.getPrimaryColor());
        totalRoomsLabel = (JLabel) ((JPanel) totalPanel.getComponent(0)).getComponent(1);
        panel.add(totalPanel);

        // 空闲房间
        JPanel availablePanel = createStatCard("空闲房间", "0", HotelManagementSystem.getSuccessColor());
        availableRoomsLabel = (JLabel) ((JPanel) availablePanel.getComponent(0)).getComponent(1);
        panel.add(availablePanel);

        // 已入住房间
        JPanel occupiedPanel = createStatCard("已入住", "0", HotelManagementSystem.getAccentColor());
        occupiedRoomsLabel = (JLabel) ((JPanel) occupiedPanel.getComponent(0)).getComponent(1);
        panel.add(occupiedPanel);

        // 已预定房间
        JPanel reservedPanel = createStatCard("已预定", "0", new Color(243, 156, 18));
        reservedRoomsLabel = (JLabel) ((JPanel) reservedPanel.getComponent(0)).getComponent(1);
        panel.add(reservedPanel);

        return panel;
    }

    private JPanel createStatCard(String title, String value, Color color) {
        JPanel card = new JPanel(new BorderLayout());
        card.setBackground(Color.WHITE);
        card.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(color, 2),
                new EmptyBorder(15, 15, 15, 15)));

        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.setBackground(Color.WHITE);

        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        titleLabel.setForeground(Color.GRAY);
        contentPanel.add(titleLabel, BorderLayout.NORTH);

        JLabel valueLabel = new JLabel(value);
        valueLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        valueLabel.setForeground(color);
        contentPanel.add(valueLabel, BorderLayout.CENTER);

        card.add(contentPanel);
        return card;
    }

    private JPanel createTablePanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);

        // 表格标题
        JLabel tableTitle = new JLabel("房间详细信息");
        tableTitle.setFont(new Font("微软雅黑", Font.BOLD, 16));
        tableTitle.setForeground(HotelManagementSystem.getPrimaryColor());
        tableTitle.setBorder(new EmptyBorder(0, 0, 10, 0));
        panel.add(tableTitle, BorderLayout.NORTH);

        // 创建表格
        String[] columnNames = { "房间号", "房间类型", "价格(元/晚)", "状态" };
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        roomTable = new JTable(tableModel);
        roomTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        roomTable.setRowHeight(30);
        roomTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
        roomTable.getTableHeader().setBackground(HotelManagementSystem.getBackgroundColor());
        roomTable.setSelectionBackground(HotelManagementSystem.getSecondaryColor().brighter());

        // 设置状态列的渲染器
        roomTable.getColumnModel().getColumn(3).setCellRenderer(new StatusCellRenderer());

        JScrollPane scrollPane = new JScrollPane(roomTable);
        scrollPane.setPreferredSize(new Dimension(0, 300));
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    private JButton createStyledButton(String text, Color backgroundColor) {
        JButton button = new JButton(text);
        button.setFont(HotelManagementSystem.getButtonFont());
        button.setBackground(backgroundColor);
        button.setForeground(Color.WHITE);
        button.setBorder(new EmptyBorder(8, 15, 8, 15));
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 添加鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(backgroundColor.darker());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(backgroundColor);
            }
        });

        return button;
    }

    public void refreshData() {
        List<Room> rooms = DataManager.getInstance().getAllRooms();

        // 清空表格
        tableModel.setRowCount(0);

        // 统计数据
        int total = rooms.size();
        int available = 0;
        int occupied = 0;
        int reserved = 0;

        // 填充表格数据
        for (Room room : rooms) {
            Object[] rowData = {
                    room.getRoomNumber(),
                    room.getRoomType(),
                    "¥" + room.getPrice(),
                    room.getStatus()
            };
            tableModel.addRow(rowData);

            // 统计状态
            switch (room.getStatus()) {
                case "空闲":
                    available++;
                    break;
                case "已入住":
                    occupied++;
                    break;
                case "已预定":
                    reserved++;
                    break;
            }
        }

        // 更新统计标签
        totalRoomsLabel.setText(String.valueOf(total));
        availableRoomsLabel.setText(String.valueOf(available));
        occupiedRoomsLabel.setText(String.valueOf(occupied));
        reservedRoomsLabel.setText(String.valueOf(reserved));
    }

    // 状态单元格渲染器
    private class StatusCellRenderer extends JLabel implements TableCellRenderer {
        public StatusCellRenderer() {
            setOpaque(true);
            setHorizontalAlignment(CENTER);
            setFont(new Font("微软雅黑", Font.BOLD, 12));
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            String status = value.toString();
            setText(status);

            if (isSelected) {
                setBackground(table.getSelectionBackground());
                setForeground(table.getSelectionForeground());
            } else {
                setForeground(Color.WHITE);
                switch (status) {
                    case "空闲":
                        setBackground(HotelManagementSystem.getSuccessColor());
                        break;
                    case "已入住":
                        setBackground(HotelManagementSystem.getAccentColor());
                        break;
                    case "已预定":
                        setBackground(new Color(243, 156, 18));
                        break;
                    default:
                        setBackground(Color.GRAY);
                        break;
                }
            }

            return this;
        }
    }
}